﻿using System;
using System.Linq;
using System.Xml.Linq;
using FluentAssertions;
using LINQPad.Extensibility.DataContext;
using LINQPad.OpenAccess.Services;
using LINQPad.OpenAccess.Settings;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Telerik.OpenAccess.Metadata;

namespace LINQPad.OpenAccess.Tests.Services
{
    [TestClass]
    public class DataContextSettingsRepositoryFixture
    {
        #region - helpers

        /// <summary>
        /// Creates the repository.
        /// </summary>
        /// <returns>The repository.</returns>
        private DataContextSettingsRepository CreateRepository( )
        {
            return new DataContextSettingsRepository( );
        }
        /// <summary>
        /// Creates the connection info mock.
        /// </summary>
        /// <param name="driverData">The driver data.</param>
        /// <returns>
        /// The connection info mock.
        /// </returns>
        private Mock<IConnectionInfo> CreateConnectionInfoMock( XElement driverData )
        {
            var mock = new Mock<IConnectionInfo>( );

            mock.SetupGet( p => p.DriverData ).Returns( ( ) => driverData );
            mock.SetupSet( p => p.DriverData ).Callback( d => driverData = d );

            return mock;
        }

        #endregion

        [TestMethod]
        public void GetSettingsReturnsNullIfNoDriverDataIsAvailable( )
        {
            var repos = this.CreateRepository( );

            var emptyConnectionInfo = this.CreateConnectionInfoMock( driverData: null );

            var emptySettings = repos.GetSettings( emptyConnectionInfo.Object );

            emptySettings.Should( ).BeNull( );
        }
        [TestMethod]
        public void TestGetAndSaveSettings( )
        {
            var repos = this.CreateRepository( );

            var cxInfoMock = this.CreateConnectionInfoMock( driverData: null );
            var settings = new DriverSettings
                                {
                                    MetadataSource = new MetadataSourceSettings { MappingType = MappingDefinitionType.Xml, Name = "XmlTest", Path = "Root" }
                                };

            // save
            repos.SaveSettings( cxInfoMock.Object, settings );

            cxInfoMock.VerifySet( p => p.DriverData );
            cxInfoMock.Object.DriverData.Should( ).NotBeNull( );

            // get
            var clonedSettings = repos.GetSettings( cxInfoMock.Object );

            cxInfoMock.VerifyGet( p => p.DriverData );
            clonedSettings.Should( ).NotBeNull( );
            clonedSettings.ShouldHave( )
                          .IncludingNestedObjects( )
                          .AllProperties( )
                          .EqualTo( settings );
        }
        [TestMethod]
        public void SaveSettingsShouldRemoveDriverDataIfDriverSettingsIsNull( )
        {
            var repos = this.CreateRepository( );
            var cxInfoMock = this.CreateConnectionInfoMock( driverData: new XElement( "dummy" ) );

            repos.Invoking( r => r.SaveSettings( cxInfoMock.Object, null ) )
                 .ShouldNotThrow<ArgumentNullException>( );

            cxInfoMock.VerifySet( p => p.DriverData );
            cxInfoMock.Object.DriverData.Should( ).BeNull( );
        }
        [TestMethod]
        public void GetSettingsThrowsArgumentNullExceptionIfConnectionInfoIsNull( )
        {
            var repos = this.CreateRepository( );

            repos.Invoking( r => r.GetSettings( connectionInfo: null ) )
                 .ShouldThrow<ArgumentNullException>( );
        }
        [TestMethod]
        public void SaveSettingsThrowsArgumentNullExceptionIfConnectionInfoIsNull( )
        {
            var repos = this.CreateRepository( );

            repos.Invoking( r => r.SaveSettings( null, new DriverSettings( ) ) )
                 .ShouldThrow<ArgumentNullException>( );
        }
    }
}
